package com.exp.controller;

import com.exp.dao.UserMapper;
import com.exp.model.User;
import com.exp.model.more.MoreUser;
import com.exp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * Created by P0015475 on 2015/9/11.
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/view")
    public String view() {
        return "user/list";
    }

    @RequestMapping("/editUI")
    public String editUI() {
        return "user/editUI";
    }

    @RequestMapping("/list")
    @ResponseBody
    public List<MoreUser> list(@RequestParam Map<String, Object> params) {
        List<MoreUser> users = userService.getList(params);
        return users;
    }
    @RequestMapping("/getUser/{id}")
    @ResponseBody
    public User getUser(@PathVariable Integer id){
        User user = null;
        if(id!=null){
            user = userMapper.selectByPrimaryKey(id);
        }
        return user;
    }
    @RequestMapping("/delete/{id}")
    @ResponseBody
    public boolean delete(@PathVariable Integer id) {
        userMapper.deleteByPrimaryKey(id);
        return true;
    }
    @RequestMapping("/edit")
    @ResponseBody
    public boolean edit(User user){
            if(user.getId()!=null){
                userMapper.updateByPrimaryKeySelective(user);
            }else{
                userMapper.insertSelective(user);
            }
            return true;
    }
}
